/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dungnaServlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.DateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import helper.ConnectionHelper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;

/**
 *
 * @author AnhDung
 */
@WebServlet(name="RegisterServlet", urlPatterns={"/RegisterServlet"})
public class RegisterServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        

        String username = request.getParameter("txtloginname");
        String password = request.getParameter("txtpassword");
        String repassword = request.getParameter("txtrepassword");
        String fname = request.getParameter("txtfname");
        String lname = request.getParameter("txtlname");
        String address = request.getParameter("txtaddress");
        //Boolean sex = request.getParameter(lname);
        Boolean gender = request.getParameter("RadioGroup1").equals("true");

        HttpSession session = request.getSession();

        //Date regTime = new java.sql.Date(date);
        //Date LastAccessed = new Date(session.getLastAccessedTime());
        Date now = new Date();
        SimpleDateFormat SDP = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
       // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-dd-MM yyyy HH:mm:ss.zzz");
        String snow =SDP.format( now );
       // String snow1=sdf.format( now );
       // String snow2 = request.gets
      //  DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);

        try {
String str = request.getParameter("txtloginname");
Connection con;
// Get a Connection to the database
con = ConnectionHelper.GetConnection();
//Add the data into the database
String sql = "SELECT UserName FROM Users WHERE UserName='"+str+"'";
Statement stm = con.createStatement();
stm.executeQuery(sql);
ResultSet rs= stm.getResultSet();
int count = 0;
while (rs.next ()){
count++;
}
if(count==0)
{
String s = "INSERT INTO Users(UserName,Password,FirstName,LastName,Sex,Address,IsAdmin,RegisterDate,LastActionDate) VALUES ('" + username + "','" + password + "','" + fname + "','"+lname+"','" + gender + "','" + address + "','"+false+"','" + snow + "','" +snow+ "')";
           //Connection con = ConnectionHelper.GetConnection();
           PreparedStatement p = con.prepareStatement(s);

          int rs1 = p.executeUpdate();
            if(rs1>0)
               {
                   response.sendRedirect("registerSuccess.jsp");
               }
           else
           {
                response.sendRedirect("index.jsp");
            }
}
else
	{
	out.println("Username is not available");
	}
}catch(Exception e){
out.println("Exception is ;"+e);
}

       
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
